﻿using System.Linq;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

namespace OpenXmlEasy.Word
{
    public class StyleSetter
    {
        // 为文档创建段落样式  
        public static void CreateParagraphStyle(WordprocessingDocument doc)
        {
            // 进入文档控制样式部分  
            var styleDefinitionsPart = doc.MainDocumentPart.AddNewPart<StyleDefinitionsPart>();
            var root = new Styles();
            root.Save(styleDefinitionsPart);

            var styles = styleDefinitionsPart.Styles;
            if (styles == null)
            {
                styleDefinitionsPart.Styles = new Styles();
                styleDefinitionsPart.Styles.Save();
            }
            if (styles == null) return;

            // 创建样式content
            {
                var style2 = new Style { Type = StyleValues.Paragraph, StyleId = "content" };
                var styleName2 = new StyleName { Val = "content" };
                var basedOn2 = new BasedOn { Val = "a" };
                var nextParagraphStyle2 = new NextParagraphStyle { Val = "a" };
                var linkedStyle2 = new LinkedStyle { Val = "2Char" };
                var uIPriority2 = new UIPriority { Val = 9 };
                var unhideWhenUsed2 = new UnhideWhenUsed();
                var primaryStyle2 = new PrimaryStyle();
                var rsid2 = new Rsid { Val = "00B74129" };
                var spacingBetweenLines1 = new SpacingBetweenLines { Line = "360", LineRule = LineSpacingRuleValues.Auto };

                var styleParagraphProperties2 = new StyleParagraphProperties();
                var keepNext2 = new KeepNext();
                var keepLines2 = new KeepLines();
                var spacingBetweenLines2 = new SpacingBetweenLines { Before = "260", After = "260", Line = "416", LineRule = LineSpacingRuleValues.Auto };
                var outlineLevel2 = new OutlineLevel { Val = 1 };
                styleParagraphProperties2.Append(new OpenXmlElement[]
                {
                    keepNext2,keepLines2,spacingBetweenLines2,outlineLevel2,spacingBetweenLines1
                });
                var styleRunProperties2 = new StyleRunProperties();
                var runFonts2 = new RunFonts { AsciiTheme = ThemeFontValues.MajorHighAnsi, HighAnsiTheme = ThemeFontValues.MajorHighAnsi, EastAsiaTheme = ThemeFontValues.MajorEastAsia, ComplexScriptTheme = ThemeFontValues.MajorBidi };
                var bold2 = new Bold();
                var boldComplexScript2 = new BoldComplexScript();
                var fontSize2 = new FontSize { Val = "32" };
                var fontSizeComplexScript2 = new FontSizeComplexScript { Val = "32" };
                styleRunProperties2.Append(new OpenXmlElement[]
                {
                    runFonts2,bold2,boldComplexScript2,fontSize2,fontSizeComplexScript2
                });
                style2.Append(new OpenXmlElement[]
                {
                    styleName2,basedOn2,nextParagraphStyle2,linkedStyle2,uIPriority2,
                   unhideWhenUsed2 ,primaryStyle2,rsid2,styleParagraphProperties2,styleRunProperties2
                });
                styles.AppendChild(style2);
            }
            //表格样式
            {
                var style1 = new Style { Type = StyleValues.Table, StyleId = "table" };
                var styleName1 = new StyleName { Val = "Table Grid" };
                var basedOn1 = new BasedOn { Val = "a1" };
                var uIPriority1 = new UIPriority { Val = 59 };
                var rsid1 = new Rsid { Val = "00AB0518" };

                var styleTableProperties1 = new StyleTableProperties();
                var tableIndentation1 = new TableIndentation { Width = 0, Type = TableWidthUnitValues.Dxa };

                var tableBorders1 = new TableBorders();
                var topBorder1 = new TopBorder { Val = BorderValues.Single, Color = "auto", Size = 4U, Space = 0U };
                var leftBorder1 = new LeftBorder { Val = BorderValues.Single, Color = "auto", Size = 4U, Space = 0U };
                var bottomBorder1 = new BottomBorder { Val = BorderValues.Single, Color = "auto", Size = 4U, Space = 0U };
                var rightBorder1 = new RightBorder { Val = BorderValues.Single, Color = "auto", Size = 4U, Space = 0U };
                var insideHorizontalBorder1 = new InsideHorizontalBorder { Val = BorderValues.Single, Color = "auto", Size = 4U, Space = 0U };
                var insideVerticalBorder1 = new InsideVerticalBorder { Val = BorderValues.Single, Color = "auto", Size = 4U, Space = 0U };

                tableBorders1.AppendChild(topBorder1);
                tableBorders1.AppendChild(leftBorder1);
                tableBorders1.AppendChild(bottomBorder1);
                tableBorders1.AppendChild(rightBorder1);
                tableBorders1.AppendChild(insideHorizontalBorder1);
                tableBorders1.AppendChild(insideVerticalBorder1);

                var tableCellMarginDefault1 = new TableCellMarginDefault();
                var topMargin1 = new TopMargin { Width = "0", Type = TableWidthUnitValues.Dxa };
                var tableCellLeftMargin1 = new TableCellLeftMargin { Width = 108, Type = TableWidthValues.Dxa };
                var bottomMargin1 = new BottomMargin { Width = "0", Type = TableWidthUnitValues.Dxa };
                var tableCellRightMargin1 = new TableCellRightMargin { Width = 108, Type = TableWidthValues.Dxa };

                tableCellMarginDefault1.AppendChild(topMargin1);
                tableCellMarginDefault1.AppendChild(tableCellLeftMargin1);
                tableCellMarginDefault1.AppendChild(bottomMargin1);
                tableCellMarginDefault1.AppendChild(tableCellRightMargin1);

                styleTableProperties1.AppendChild(tableIndentation1);
                styleTableProperties1.AppendChild(tableBorders1);
                styleTableProperties1.AppendChild(tableCellMarginDefault1);

                style1.AppendChild(styleName1);
                style1.AppendChild(basedOn1);
                style1.AppendChild(uIPriority1);
                style1.AppendChild(rsid1);
                style1.AppendChild(styleTableProperties1);

                styles.AppendChild(style1);
            }
            {
                styles.AppendChild(GenerateStyleOfTitle1());
                styles.AppendChild(GenerateStyleOfTitle2());
                styles.AppendChild(GenerateStyleOfTitle3());
            }
        }

        public static Style GenerateStyleOfTitle1()
        {
            var style1 = new Style { Type = StyleValues.Paragraph, StyleId = "heading 1" };
            var styleName1 = new StyleName { Val = "heading 1" };
            var basedOn1 = new BasedOn { Val = "a" };
            var nextParagraphStyle1 = new NextParagraphStyle { Val = "a" };
            var linkedStyle1 = new LinkedStyle { Val = "1Char" };
            var uIPriority1 = new UIPriority { Val = 9 };
            var primaryStyle1 = new PrimaryStyle();
            var rsid1 = new Rsid { Val = "00A204AD" };

            var styleParagraphProperties1 = new StyleParagraphProperties();
            var keepNext1 = new KeepNext();
            var keepLines1 = new KeepLines();
            var spacingBetweenLines1 = new SpacingBetweenLines { Before = "340", After = "330", Line = "578", LineRule = LineSpacingRuleValues.Auto };
            var outlineLevel1 = new OutlineLevel { Val = 0 };

            styleParagraphProperties1.AppendChild(keepNext1);
            styleParagraphProperties1.AppendChild(keepLines1);
            styleParagraphProperties1.AppendChild(spacingBetweenLines1);
            styleParagraphProperties1.AppendChild(outlineLevel1);

            var styleRunProperties1 = new StyleRunProperties();
            var bold1 = new Bold();
            var boldComplexScript1 = new BoldComplexScript();
            var kern1 = new Kern { Val = 44U };
            var fontSize1 = new FontSize { Val = "44" };
            var fontSizeComplexScript1 = new FontSizeComplexScript { Val = "44" };

            styleRunProperties1.AppendChild(bold1);
            styleRunProperties1.AppendChild(boldComplexScript1);
            styleRunProperties1.AppendChild(kern1);
            styleRunProperties1.AppendChild(fontSize1);
            styleRunProperties1.AppendChild(fontSizeComplexScript1);

            style1.AppendChild(styleName1);
            style1.AppendChild(basedOn1);
            style1.AppendChild(nextParagraphStyle1);
            style1.AppendChild(linkedStyle1);
            style1.AppendChild(uIPriority1);
            style1.AppendChild(primaryStyle1);
            style1.AppendChild(rsid1);
            style1.AppendChild(styleParagraphProperties1);
            style1.AppendChild(styleRunProperties1);
            return style1;
        }

        public static Style GenerateStyleOfTitle2()
        {
            var style1 = new Style { Type = StyleValues.Paragraph, StyleId = "heading 2" };
            var styleName1 = new StyleName { Val = "heading 2" };
            var basedOn1 = new BasedOn { Val = "a" };
            var nextParagraphStyle1 = new NextParagraphStyle { Val = "a" };
            var linkedStyle1 = new LinkedStyle { Val = "2Char" };
            var uIPriority1 = new UIPriority { Val = 9 };
            var unhideWhenUsed1 = new UnhideWhenUsed();
            var primaryStyle1 = new PrimaryStyle();
            var rsid1 = new Rsid { Val = "00A204AD" };

            var styleParagraphProperties1 = new StyleParagraphProperties();
            var keepNext1 = new KeepNext();
            var keepLines1 = new KeepLines();
            var spacingBetweenLines1 = new SpacingBetweenLines { Before = "260", After = "260", Line = "416", LineRule = LineSpacingRuleValues.Auto };
            var outlineLevel1 = new OutlineLevel { Val = 1 };

            styleParagraphProperties1.AppendChild(keepNext1);
            styleParagraphProperties1.AppendChild(keepLines1);
            styleParagraphProperties1.AppendChild(spacingBetweenLines1);
            styleParagraphProperties1.AppendChild(outlineLevel1);

            var styleRunProperties1 = new StyleRunProperties();
            var runFonts1 = new RunFonts { AsciiTheme = ThemeFontValues.MajorHighAnsi, HighAnsiTheme = ThemeFontValues.MajorHighAnsi, EastAsiaTheme = ThemeFontValues.MajorEastAsia, ComplexScriptTheme = ThemeFontValues.MajorBidi };
            var bold1 = new Bold();
            var boldComplexScript1 = new BoldComplexScript();
            var fontSize1 = new FontSize { Val = "32" };
            var fontSizeComplexScript1 = new FontSizeComplexScript { Val = "32" };

            styleRunProperties1.AppendChild(runFonts1);
            styleRunProperties1.AppendChild(bold1);
            styleRunProperties1.AppendChild(boldComplexScript1);
            styleRunProperties1.AppendChild(fontSize1);
            styleRunProperties1.AppendChild(fontSizeComplexScript1);

            style1.AppendChild(styleName1);
            style1.AppendChild(basedOn1);
            style1.AppendChild(nextParagraphStyle1);
            style1.AppendChild(linkedStyle1);
            style1.AppendChild(uIPriority1);
            style1.AppendChild(unhideWhenUsed1);
            style1.AppendChild(primaryStyle1);
            style1.AppendChild(rsid1);
            style1.AppendChild(styleParagraphProperties1);
            style1.AppendChild(styleRunProperties1);
            return style1;

        }

        public static Style GenerateStyleOfTitle3()
        {
            var style1 = new Style { Type = StyleValues.Paragraph, StyleId = "heading 3" };
            var styleName1 = new StyleName { Val = "heading 3" };
            var basedOn1 = new BasedOn { Val = "a" };
            var nextParagraphStyle1 = new NextParagraphStyle { Val = "a" };
            var linkedStyle1 = new LinkedStyle { Val = "3Char" };
            var uIPriority1 = new UIPriority { Val = 9 };
            var unhideWhenUsed1 = new UnhideWhenUsed();
            var primaryStyle1 = new PrimaryStyle();
            var rsid1 = new Rsid { Val = "00A204AD" };

            var styleParagraphProperties1 = new StyleParagraphProperties();
            var keepNext1 = new KeepNext();
            var keepLines1 = new KeepLines();
            var spacingBetweenLines1 = new SpacingBetweenLines { Before = "260", After = "260", Line = "416", LineRule = LineSpacingRuleValues.Auto };
            var outlineLevel1 = new OutlineLevel { Val = 2 };

            styleParagraphProperties1.AppendChild(keepNext1);
            styleParagraphProperties1.AppendChild(keepLines1);
            styleParagraphProperties1.AppendChild(spacingBetweenLines1);
            styleParagraphProperties1.AppendChild(outlineLevel1);

            var styleRunProperties1 = new StyleRunProperties();
            var bold1 = new Bold();
            var boldComplexScript1 = new BoldComplexScript();
            var fontSize1 = new FontSize { Val = "32" };
            var fontSizeComplexScript1 = new FontSizeComplexScript { Val = "32" };

            styleRunProperties1.AppendChild(bold1);
            styleRunProperties1.AppendChild(boldComplexScript1);
            styleRunProperties1.AppendChild(fontSize1);
            styleRunProperties1.AppendChild(fontSizeComplexScript1);

            style1.AppendChild(styleName1);
            style1.AppendChild(basedOn1);
            style1.AppendChild(nextParagraphStyle1);
            style1.AppendChild(linkedStyle1);
            style1.AppendChild(uIPriority1);
            style1.AppendChild(unhideWhenUsed1);
            style1.AppendChild(primaryStyle1);
            style1.AppendChild(rsid1);
            style1.AppendChild(styleParagraphProperties1);
            style1.AppendChild(styleRunProperties1);
            return style1;
        }

        public void ApplyStyleToParagraph(Paragraph paragraph, string styleN)
        {
            if (!paragraph.Elements<ParagraphProperties>().Any())
            {
                paragraph.PrependChild(new ParagraphProperties());
            }
            var pPr = paragraph.ParagraphProperties;
            if (pPr.ParagraphStyleId == null)
                pPr.ParagraphStyleId = new ParagraphStyleId();
            pPr.ParagraphStyleId.Val = styleN;
        }

    }
}
